/**
 * @Description: 文件下载功能
 *
 * @author: liuxiuqian
 *
 * @date: 2019/3/5
 */
import { message } from 'antd';
import html2canvas from 'html2canvas';
import { IEexcel } from '@/services/download';
import JsEncrypt from 'jsencrypt';

export const Downer = (path: any, fileName = '') => {
  try {
    if (fileName === '预警总览') {
      const jse = new JsEncrypt();
      jse.setPublicKey(
        'MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAkeVl/vt/KdCsRBDxLp+2rt4tucFy+TNwm1zhA00IX+iE2os7MnjMeI5KprdXtck9sA1DLfKBnkwJqH2U+RsWtZQOsXpj8IUKzA1H3H67lJoxfz74h7WwvAlvlSZnNnQl7WD/5pB+VJSzeycer/9Bf8zcez+ppRmM/mOKN7KgaZELwmpyt1rbbf+WkINeBhDDU3wVYCW+MEeqVRVkYWWgL7o9VkQV4F6SkfjMHwEx5xccj9F32qTXGbfZKp1d0u1k2GSS3n6qKE1NrOFESW2wf4cmGlpp/spO/Hf1gwWL/TuQZz0LqOyeqQM9S8I0ezQmHQQcJBro8AQzx7pCH5JHuwIDAQAB',
      );
      jse.setPrivateKey(
        'MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQCR5WX++38p0KxEEPEun7au3i25wXL5M3CbXOEDTQhf6ITaizsyeMx4jkqmt1e1yT2wDUMt8oGeTAmofZT5Gxa1lA6xemPwhQrMDUfcfruUmjF/PviHtbC8CW+VJmc2dCXtYP/mkH5UlLN7Jx6v/0F/zNx7P6mlGYz+Y4o3sqBpkQvCanK3Wttt/5aQg14GEMNTfBVgJb4wR6pVFWRhZaAvuj1WRBXgXpKR+MwfATHnFxyP0XfapNcZt9kqnV3S7WTYZJLefqooTU2s4URJbbB/hyYaWmn+yk78d/WDBYv9O5BnPQuo7J6pAz1LwjR7NCYdBBwkGujwBDPHukIfkke7AgMBAAECggEAQIgzQUGE0b4XNFQ6GeFTMK3dk9hZPpjeKho+/PLJzYJPoUsMeBqudnSZ2HwoM6Q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PnTn90i36TkD11iV68g++fXYpr1UgzB/7NfgLDMIJS1b3NclHr3BvmqpokRG9YFNcjRcKwnCA4aHab3rBlQ/g==',
      );
      const url = jse.decrypt(path);
      // eslint-disable-next-line no-param-reassign
      path = url;
    }
    const elink = document.createElement('a');
    // elink.download = path;
    elink.style.display = 'none';
    elink.href = path;
    document.body.appendChild(elink);
    elink.click();
    URL.revokeObjectURL(elink.href);
    document.body.removeChild(elink);
  } catch (e) {
    message.error('下载异常！');
  }
  // try {
  //   const elemIF = document.createElement('iframe');
  //   elemIF.src = path;
  //   elemIF.style.display = 'none';
  //   console.log(elemIF)
  //   document.body.appendChild(elemIF);
  // } catch (e) {
  //   message.error("下载异常！")
  // }
  // window.open(path, '_self');
  return true;
};

const fetchFun = (paramJson: any) => {
  IEexcel(paramJson).then((res: any) => {
    if (res?.path !== '') {
      // window.open(res.path, '_self');
      Downer(res.path, paramJson.fileName);
    } else {
      message.error('下载失败！');
    }
  });
};

const jsonData = (json: any, pic = '') => {
  const newJson = json;
  newJson.path = '';
  if (pic) {
    newJson.pic = { imageBase64: pic };
  }
  return newJson;
};

export default function DownloadFile(json: any, element = '') {
  if (element) {
    // @ts-ignore
    const imgBase64 = html2canvas(element, {
      allowTaint: true, // 允许跨域
      async: false, // 异步
      removeContaine: true, // 是否清理克隆的DOM元素html2canvas暂时创建
    }).then((canvas: any) => canvas.toDataURL());
    imgBase64.then((res: any) => {
      fetchFun(jsonData(json, res));
    });
  } else {
    fetchFun(jsonData(json));
  }
}
